package com.example.taobao

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_mine.*
import kotlinx.android.synthetic.main.newaddress.*
import okhttp3.FormBody
import okhttp3.FormBody.*
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.ResponseBody
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.BufferedReader
import java.io.DataOutputStream
import java.io.InputStreamReader
import java.lang.Exception
import java.lang.StringBuilder
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLEncoder
import kotlin.concurrent.thread

class Mine : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mine)
        val gson = Gson()
        var msglistd:ArrayList<MsgContentBean> = ArrayList<MsgContentBean>()
        msglistd.add(MsgContentBean(1,"晚上去看电影啊"))
        msglistd.add(MsgContentBean(1,"有空吗"))
        msglistd.add(MsgContentBean(2,"我看看有没有时间吧，我很忙的"))
        val inputString = gson.toJson(msglistd)
        tv_btn.setOnClickListener {
            thread {
                try {
                    val client = OkHttpClient()
                    val requestBody = Builder()
                        .add("name", URLEncoder.encode("何俊","utf-8"))
                        .add("history",URLEncoder.encode(inputString,"utf-8"))
                        .add("mas",URLEncoder.encode("15267651805","utf-8"))
                        .add("time",URLEncoder.encode("临安","utf-8"))
                        .add("username",URLEncoder.encode("Eurus","utf-8"))
                        .build()
                    val request = Request.Builder()
                        .url("http://192.168.227.1:8080/31232/megadd.jsp")
                        .post(requestBody)
                        .build()
                    val respone = client.newCall(request).execute()
                    val responeData = respone.body?.string()
                    if (responeData!=null)
                    {
                        runOnUiThread{
                            show.text = "添加成功"
                        }
                    }
                    else
                    { show.text = "添加失败"

                    }
                }
                catch (e:Exception)
                {
                    e.printStackTrace()
                }
            }
        }
    }
}